package com.china08.yxyapi.api.manage.stu.mystudy.huixue.scoreimprove;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 成绩提升 计划查看
 * 
 */
@CrossOrigin
@RestController
public class HuiXueScoreImproveCheckController extends YxyApiBaseController {

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @ResponseBody
    @ApiOperation(value = "成绩提升首页 计划查看")
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/scoreimprove/plandetail/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {
        RespModel model = new RespModel();
        YxyHuixuePlan yxyHuixuePlan = yxyHuixuePlanRepository.findById(reqModel.planId);
        if (yxyHuixuePlan != null) {
            model.lastScoreType = yxyHuixuePlan.getLastScoreType(); // 上次考试分值（尺子的总长度）,
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 开始时间
            Integer beginDate = yxyHuixuePlan.getBeginDate();
            String beginDateString = null;
            Date beginDate2 = null;
            if (beginDate != null && beginDate != 0) {
                beginDateString = String.valueOf(beginDate);
                beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                        + beginDateString.substring(6);
                beginDate2 = sdf.parse(beginDateString);
            }
            // 结束时间
            Integer endDate = yxyHuixuePlan.getEndDate();
            String endDateString = null;
            Date endDate2 = null;
            if (endDate != null && endDate != 0) {
                endDateString = String.valueOf(endDate);
                endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                        + endDateString.substring(6);
                endDate2 = sdf.parse(endDateString);
            }
            model.usetime = this.getIntervalDays(beginDate2, endDate2) + 1;// 学习计划用时
            model.subject = yxyHuixuePlan.getSubjectName();// 学科名字
            model.semesterName = yxyHuixuePlan.getSemesterName();// 册别名字
            model.charName = yxyHuixuePlan.getBookContentName();// 学习计划中选择的章
        } else {
            model.lastScoreType = 0; // 上次考试分值（尺子的总长度）, 100,120,150,180
            model.usetime = 0;// 学习计划用时
            model.subject = "";// 学科名字
            model.semesterName = "";// 册别名字
            model.charName = "";// 学习计划中选择的章
        }
        return model;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 上次考试分值
        public Integer lastScoreType;// 上次考试成绩分值（尺子的总长度）, 100,120,150,180
        // 学习计划用时
        public Integer usetime;
        // 学习计划中的学科名
        public String subject;
        // 学习计划中的册别
        public String semesterName;
        // 学习计划中选择的章
        public String charName;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 计划id
        public String planId;
        // 学生id
        public String studentId;
    }

    private static Integer getIntervalDays(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {

        return -1;

        }

        long intervalMilli = oDate.getTime() - fDate.getTime();

        return (int) (intervalMilli / (24 * 60 * 60 * 1000));

    }

}
